﻿#include "ColorRenderPass.h"

#include "Asset/AssetManager.h"

ColorRenderPass::ColorRenderPass(const Ref<const Shader>& shader) : shader(shader) {
  assert(shader != nullptr);
}

void ColorRenderPass::SetTexture(const std::string& attachmentName, const Ref<Texture>& texture, int32_t slot) {
  shader->SetTexture(attachmentName, texture, slot);
}

void ColorRenderPass::Render() {
  shader->Bind();
  glDisable(GL_DEPTH_TEST);
  FullscreenQuad::GetVertexArray()->DrawIndexed();
  glEnable(GL_DEPTH_TEST);
}

void ColorRenderPass::RenderTextureWithEffect(const Ref<Texture>& texture, const Ref<const Shader>& effect) {
  ColorRenderPass renderPass(effect);
  renderPass.SetTexture("uColorTexture", texture, 0);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  renderPass.Render();
}

void ColorRenderPass::RenderTexture(const Ref<Texture>& texture) {
  static Ref<Shader> colorIdentity = AssetManager::LoadAsset<Shader>("assets/shaders/src/Identity");
  RenderTextureWithEffect(texture, colorIdentity);
}
